package com.mybatis_puls.demo.controller;


import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mybatis_puls.demo.entity.R;
import com.mybatis_puls.demo.entity.User;
import com.mybatis_puls.demo.entity.Withdraw;
import com.mybatis_puls.demo.service.UserService;
import com.mybatis_puls.demo.service.WithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小🌤
 * @since 2020-12-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private WithdrawService withdrawService;

    @Resource
    private UserService userService;

    @RequestMapping("/save")
    public Object s( User user){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
         wrapper.eq("phone",user.getPhone());
        List<User> list = userService.list(wrapper);
        if(list.size()>0){
           return  list.get(0);
        }
        userService.save(user);
    return user;
    }

    @RequestMapping("/update")
    public Object uodate(User user){
        List<User> users1 = userService.listByIds(Collections.singleton(user.getId()));
         //是提现
        if(users1.get(0).getShowFee()>user.getShowFee()){
            Withdraw withdraw=new Withdraw();
            withdraw.setAmount(users1.get(0).getShowFee()-user.getShowFee());
            withdraw.setTime(new Date());
            withdraw.setUuid(IdUtil.simpleUUID());
            withdraw.setUserId(user.getId());
             withdrawService.save(withdraw);
        }
        userService.updateById(user);

        List<User> users = userService.listByIds(Arrays.asList(user.getId()));
        return users.get(0);
    }

     @RequestMapping("/info/{id}")
    public Object info(@PathVariable("id")Integer id){
         List<User> users = userService.listByIds(Collections.singleton(id));
          return  users.get(0);
    }


    /**
     * 文件上传
     * @param file
     * @return
     */
    @RequestMapping("/updoladfile")
    public Object UPpload(MultipartFile file,Integer id) {
        String  newFileName=null;
        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            // *只有当满足图片格式时才进来，重新赋图片名，防止出现名称重复的情况
            String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
           newFileName = IdUtil.simpleUUID()+"."+fileSuffix;
            // 该方法返回的为当前项目的工作目录，即在哪个地方启动的java线程
            String dirPath = System.getProperty("user.dir");
            String path = File.separator + "src/main/resources/static/images/user/" + File.separator + newFileName;
            File destFile = new File(dirPath + path);
            newFileName="/images/user/"+newFileName;
            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            try {
                file.transferTo(destFile);
                // *将相对路径返回给前端
            } catch (IOException e) {
                return new R().setCode(400);
            }
            User byId = userService.getById(id);
             User user=new User();
             user.setShowFee(byId.getShowFee());
            user.setId(id);
            user.setAvatarUrl(newFileName);
             userService.updateById(user);

        }

        return newFileName;
    }

    /**
     * 修改学校
     * @param user
     * @return
     */
     @RequestMapping("/infoUser")
    public Object updateUSER(User user){
         User byId = userService.getById(user.getId());
         user.setShowFee(byId.getShowFee());
         userService.updateById(user);
         return user;
     }


}

